package com.xuewei.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    public static final String testQueueDLX ="testQueueDLX";
    public static final String queueDLX ="queueDLX";
    public static final String testExchangeDlx="testExchangeDlx";
    public static final String exchangeDlx="exchangeDlx";

    @Bean("testQueueDLX")
    public Queue testQueueDLX(){
        return QueueBuilder.durable(testQueueDLX)
                .withArgument("x-dead-letter-exchange","exchangeDlx")
                .withArgument("x-dead-letter-routing-key","dlx.hebe")
                .withArgument("x-message-ttl",10000)
                .withArgument("x-max-length",30).build();
    }

    @Bean("testExchangeDlx")
    public Exchange testExchangeDlx(){
        return ExchangeBuilder.topicExchange(testExchangeDlx).durable(true).build();
    }

    @Bean
    public Binding bindTestQueueExchange(@Qualifier("testQueueDLX") Queue queue, @Qualifier("testExchangeDlx") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("test.dlx.*").noargs();
    }

    @Bean("queueDLX")
    public Queue queueDLX(){
        return QueueBuilder.durable(queueDLX).build();
    }

    @Bean("exchangeDlx")
    public Exchange exchangeDlx(){
        return ExchangeBuilder.topicExchange(exchangeDlx).durable(true).build();
    }

    @Bean
    public Binding bindQueueExchange(@Qualifier("queueDLX") Queue queue, @Qualifier("exchangeDlx") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx.*").noargs();
    }
}
